package nl.justobjects.pushlet.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * Randomizing routines.
 *
 * @author Just van den Broecke
 * @version $Id: Rand.java,v 1.3 2007/11/23 21:10:17 justb Exp $
 */
public class Rand {
	private static char CHARS[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	private static char NON_VOWELS_CHARS[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z'};
	private static char VOWELS_CHARS[] = {'a', 'e', 'i', 'o', 'u', 'y'};
	private static char NUMBER_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	private static char OTHER_CHARS[] = {'-', '_', '@', '#', '$', '%', '&' , '*', '\\', '/'};

	private static String TEMP_FLAG = "pushlet";
	
	private static Random random = new Random();

	public static char randomChar() {
		return CHARS[randomInt(0, CHARS.length - 1)];
	}

	public static char randomVowelChar() {
		return VOWELS_CHARS[randomInt(0, VOWELS_CHARS.length - 1)];
	}

	public static char randomNonVowelChar() {
		return NON_VOWELS_CHARS[randomInt(0, NON_VOWELS_CHARS.length - 1)];
	}

	public static File randomTempDir() throws Exception {
		File directory = new File(System.getProperty("java.io.tmpdir") + File.separator + TEMP_FLAG + File.separator + randomString(16));
		directory.mkdirs();
		directory.deleteOnExit();
		return directory;
	}

	public static File randomTempFile() throws Exception {
		return randomTempFile(System.getProperty("java.io.tmpdir"));
	}

	public static File randomTempFile(String tmpDir) throws Exception {
		File file = new File(tmpDir + File.separator + randomString(16));
		file.createNewFile();
		file.deleteOnExit();
		return file;
	}
	
	public static File randomBinaryFile(int size) throws Exception {
		File file = randomTempFile();
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(randomBytes(size));
		fos.close();
		return file;
	}

	public static File randomTextFile(int size) throws Exception {
		File file = randomTempFile();
		PrintWriter pw = new PrintWriter(new FileOutputStream(file));
		pw.write(randomString(size));
		pw.close();
		return file;
	}

	public static byte[] randomBytes(int size) {
		return randomBlob(size);
	}

	public static byte[] randomBlob(int size) {
		byte[] retval = new byte[size];
		for (int i = 0; i < retval.length; i++) {
			retval[i] = randomByte();
		}
		return retval;
	}

	public static byte randomByte() {
		return (byte) random.nextInt();
	}

	public static double randomDouble() {
		return random.nextDouble();
	}

	public static int randomInt() {
		return random.nextInt();
	}

	public static int randomInt(int min, int max) {
		//20091016 lugj
		//return (int) ((Math.random() * (double) (max + 1 - min)) + min);
		return (int) ((Math.random() * (double) (max - min)) + min);
	}

	public static long randomLong() {
		return random.nextLong();
	}

	public static long randomLong(long min, long max) {
		//20091016 lugj
		//return (long) ((Math.random() * (double) (max + 1L - min)) + min);
		return (long) ((Math.random() * (double) (max - min)) + min);
	}

	public static String randomName(int length) {
		StringBuffer sb = new StringBuffer(length);
		for (int i = 0; i < length; i++) {
			sb.append(i % 2 == 0 ? randomNonVowelChar() : randomVowelChar());
		}
		return sb.toString();
	}

	public static void randomSleep(long min, long max) {
		try {
			Thread.sleep(randomLong(min, max));
		} catch (InterruptedException ie) {

		}
	}

	public static String randomString(int length) {
		StringBuffer sb = new StringBuffer(length);
		for (int i = 0; i < length; i++) {
			sb.append(randomChar());
		}
		return sb.toString();
	}

	public static String randomString() {
		return randomString(randomInt());
	}

	//
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(java.util.UUID.randomUUID().toString());
		System.out.println(java.util.UUID.randomUUID().toString());
		
		int i = (int) ((0.540000000 * (double) (5 - 0)) + 0);
		System.out.println(i);
		
		System.out.println(java.net.URLDecoder.decode("ffffff%0A33333", "utf-8"));
		
        String str = java.net.URLDecoder.decode("%0D%0A", "gbk");
        // URL编码
        String str1 = java.net.URLEncoder.encode(str, "gbk"); 
        System.out.println(str);
        System.out.println("str1" + str1);

	}

}

/*
 * $Log: Rand.java,v $
 * Revision 1.3  2007/11/23 21:10:17  justb
 * add hooks for custom logging (you can override DefaultLogger in pushlet.properties)
 *
 * Revision 1.2  2004/09/03 22:35:38  justb
 * Almost complete rewrite, just checking in now
 *
 * Revision 1.1  2004/03/10 12:21:27  justb
 * *** empty log message ***
 *
 *
 */